//
//  Agora Streaming Kit
//
//  Created by ShengQiang Liu in 2020-02.
//  Copyright (c) 2020 Agora IO. All rights reserved.
//
#pragma once
#include <memory>

#include "rtmp_connection_i.h"
// #include "facilities/tools/rtc_callback.h"
#include "rtmp_sender.h"
// #include "utils/thread/base_worker.h"

namespace agora {
namespace rtc {

class RtmpLocalUserImpl;

class RtmpConnectionImpl : public IRtmpConnectionEx, public IRtmpSenderObserver {
 public:
  RtmpConnectionImpl();
  virtual ~RtmpConnectionImpl();

 public:  // inherit from IRtmpConnection
  int connect(const char* url) override;
  int disconnect() override;
  RtmpConnectionInfo getConnectionInfo() override;
  IRtmpLocalUser* getRtmpLocalUser() override;
  int registerObserver(IRtmpConnectionObserver* observer,
                       void (*safeDeleter)(IRtmpConnectionObserver*) = NULL) override;
  int unregisterObserver(IRtmpConnectionObserver* observer) override;

 public:  // inherit from IRtmpConnectionEx
  int initialize(const RtmpConnectionConfiguration& config) override;
  int deinitialize() override;
  int SendAudioFrame(const uint8_t* data, size_t size, int64_t pts_ms) override;
  int SendVideoFrame(const uint8_t* data, size_t size, int64_t pts_ms, bool is_key) override;

 public:  // inherit from IRtmpSenderObserver
  void OnRtmpSocketStateChanged(RtmpSocketState status) override;
  void OnRtmpSenderStatisticsInfo(RtmpSenderStatisticsInfo statistics_info) override;
  void OnRtmpStreamingBufferState(RtmpStreamingBufferState status) override;

 private:
  std::shared_ptr<IAgoraRtmpSender> rtmp_sender_;
  std::unique_ptr<RtmpLocalUserImpl> rtmp_local_user_;
  utils::RtcSyncCallback<IRtmpConnectionObserver>::Type connection_observers_;
  RTMP_CONNECTION_STATE connection_state_;
  bool enable_write_flv_file_;
  int64_t last_audio_pts_ms_;
  bool is_ready_;
};

}  // namespace rtc
}  // namespace agora
